<?php
class finance extends core {
	public function __construct() {
		parent::__construct();
		$this->order_mod = m("order");
	}

	//index
	public function index() {
		$where = '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$project_id = (isset($_GET['project_id'])) ? intval($_GET['project_id']) : 0;
		$shop_id = (isset($_GET['shop_id'])) ? intval($_GET['shop_id']) : 0;
		$begin_date = (isset($_GET['begin_date'])) ? trim($_GET['begin_date']) : '';
		$end_date = (isset($_GET['end_date'])) ? trim($_GET['end_date']) : '';
		$status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
		$is_jiesuan = (isset($_GET['is_jiesuan'])) ? trim($_GET['is_jiesuan']) : '';
		$pay_method = (isset($_GET['pay_method'])) ? trim($_GET['pay_method']) : '';
		$order_id = (isset($_GET['order_id']) && trim($_GET['order_id'])) ? trim($_GET['order_id']) : '';
		
		if ($keyword) {
			$where .= " AND (order_sn LIKE '%{$keyword}%' OR o.name LIKE '%{$keyword}%' OR o.mobile LIKE '%{$keyword}%' OR o.address LIKE '%{$keyword}%')";
		}
		if ($project_id) {
			$where .= " AND o.project_ids LIKE '%,{$project_id},%'";
		}
		if ($shop_id) {
			$where .= " AND o.shop_id='{$shop_id}'";
		}
		if (strlen($begin_date)) {
			$where .= " AND o.add_time>='".strtotime($begin_date)."'";
		}
		if (strlen($end_date)) {
			$where .= " AND o.add_time<='".strtotime($end_date)."'";
		}
		if (strlen($status)) {
			$where .= " AND o.status IN ({$status})";
		}
		if (strlen($pay_method)) {
			$where .= " AND o.pay_method='{$pay_method}'";
		}
		if (strlen($is_jiesuan)) {
			$where .= " AND o.is_jiesuan='{$is_jiesuan}'";
		}
		if (trim($order_id, ',')) {
			$where .= " AND o.id IN (".trim($order_id, ',').")";
		}
		
		//分页
		$this->setpages(array('keyword'=>$keyword, 'project_id'=>$project_id, 'shop_id'=>$shop_id, 'begin_date'=>$begin_date, 'end_date'=>$end_date,
					'status'=>$status, 'is_jiesuan'=>$is_jiesuan, 'pay_method'=>$pay_method, 'order_id'=>$order_id));
		
		$sql = "SELECT o.*, s.name as shop_name, '' as pay_method_name, '' as project_names  
			FROM jf_order o LEFT JOIN jf_beautyshop s ON o.shop_id=s.id 
			WHERE 1 {$where} ORDER BY o.id DESC";
		//exit($sql);
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]['pay_method_name'] = $this->order_mod->pay_method($row['pay_method']);
				$project_names = '';
				$sql = "SELECT project_id, project_name FROM jf_order_project WHERE order_id='".$row['id']."'";
				$p = $this->db->get_results($sql);
				if ($p) {
					foreach ($p as $k => $r) {
						$project_names .= ($project_names!=''?'<br />':'') . "<a href='?app=project&act=edit&project_id={$r->project_id}' target='_blank'>";
						$project_names .= "{$r->project_name}</a>";
					}
				}
				$rs[$key]['project_names'] = $project_names;
				$rs[$key]['status_name'] = $this->order_mod->status_name($row['status']);
			}
		}
		
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		
		$filter = "";
		if (strlen($begin_date)) {
			$filter .= " AND add_time>='".strtotime($begin_date)."'";
		}
		if (strlen($end_date)) {
			$filter .= " AND add_time<='".strtotime($end_date)."'";
		}
		if (strlen($pay_method)) {
			$pay_method_en = $pay_method;
			if ($pay_method_en=='wx_h5pay') $pay_method_en = 'wxpay_h5';
			$filter .= " AND pay_method='{$pay_method_en}'";
		}
		
		//总交易金额, (订单总价+套餐购买总价)
		$sql = "SELECT SUM(total_price) FROM jf_order o WHERE status>0 {$where}";
		$price = $this->db->get_var($sql);
		$sql = "SELECT SUM(price) FROM jf_member_meal WHERE status>0 {$filter}";
		$price += $this->db->get_var($sql);
		$this->smarty->assign('total_price',$price);
		
		//微信交易总额, 同上
		$sql = "SELECT SUM(total_price) FROM jf_order o WHERE 1 AND (pay_method='wxpay' OR pay_method='wxpay_h5') AND status>0 {$where}";
		$price = $this->db->get_var($sql);
		$sql = "SELECT SUM(price) FROM jf_member_meal WHERE status>0 AND (pay_method='wxpay' OR pay_method='wx_h5pay') {$filter}";
		$price += $this->db->get_var($sql);
		$this->smarty->assign('wx_price',$price);
		
		//支付宝交易总额, 同上
		$sql = "SELECT SUM(total_price) FROM jf_order o WHERE 1 AND pay_method='alipay' AND status>0 {$where}";
		$price = $this->db->get_var($sql);
		$sql = "SELECT SUM(price) FROM jf_member_meal WHERE status>0 AND pay_method='alipay' {$filter}";
		$price += $this->db->get_var($sql);
		$this->smarty->assign('ali_price',$price);
		
		//未支付数量
		$sql = "SELECT COUNT(*) FROM jf_order o WHERE 1 AND paid_time='0' {$where}";
		$this->smarty->assign('nopay_count',$this->db->get_var($sql));
		
		//已支付数量
		$sql = "SELECT COUNT(*) FROM jf_order o WHERE 1 AND paid_time>0 {$where}";
		$this->smarty->assign('pay_count',$this->db->get_var($sql));
		
		//已取消数量
		$sql = "SELECT COUNT(*) FROM jf_order o WHERE 1 AND status='-1' {$where}";
		$this->smarty->assign('cancel_count',$this->db->get_var($sql));
		
		//已退款数量
		$sql = "SELECT COUNT(*) FROM jf_order o WHERE 1 AND status='-2' {$where}";
		$this->smarty->assign('refund_count',$this->db->get_var($sql));
		
		//已完成数量
		$sql = "SELECT COUNT(*) FROM jf_order o WHERE 1 AND status='3' {$where}";
		$this->smarty->assign('ok_count',$this->db->get_var($sql));
		
		//已结算数量
		$sql = "SELECT COUNT(*) FROM jf_order o WHERE 1 AND is_jiesuan='1' {$where}";
		$this->smarty->assign('jiesuan_count',$this->db->get_var($sql));
		
		//已使用优惠券数量
		$sql = "SELECT COUNT(*) FROM jf_order o WHERE 1 AND member_cash_coupon_id>0 {$where}";
		$this->smarty->assign('coupon_count',$this->db->get_var($sql));
		
		//全部订单数量
		$sql = "SELECT COUNT(*) FROM jf_order o WHERE 1 {$where}";
		$this->smarty->assign('order_count',$this->db->get_var($sql));
		
		$sql = "SELECT id,name FROM jf_project ORDER BY id ASC";
		$projects = $this->db->get_results($sql);
		$this->smarty->assign('projects',$projects);
		
		$sql = "SELECT id,name FROM jf_beautyshop ORDER BY id ASC";
		$shops = $this->db->get_results($sql);
		$this->smarty->assign('shops',$shops);

		$this->smarty->assign('is_admin', $_SESSION['admin']->is_admin);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('project_id',$project_id);
		$this->smarty->assign('shop_id',$shop_id);
		$this->smarty->assign('begin_date',$begin_date);
		$this->smarty->assign('end_date',$end_date);
		$this->smarty->assign('status',$status);
		$this->smarty->assign('is_jiesuan',$is_jiesuan);
		$this->smarty->assign('pay_method',$pay_method);
		$this->smarty->assign('order_id',$order_id);
		$this->display();
	}
	
	//导出excel
	public function excel() {
		$where = '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$project_id = (isset($_GET['project_id'])) ? intval($_GET['project_id']) : 0;
		$shop_id = (isset($_GET['shop_id'])) ? intval($_GET['shop_id']) : 0;
		$begin_date = (isset($_GET['begin_date'])) ? trim($_GET['begin_date']) : '';
		$end_date = (isset($_GET['end_date'])) ? trim($_GET['end_date']) : '';
		$status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
		$is_jiesuan = (isset($_GET['is_jiesuan'])) ? trim($_GET['is_jiesuan']) : '';
		$pay_method = (isset($_GET['pay_method'])) ? trim($_GET['pay_method']) : '';
		$order_id = (isset($_GET['order_id']) && trim($_GET['order_id'])) ? trim($_GET['order_id']) : '';
		
		if ($keyword) {
			$where .= " AND (order_sn LIKE '%{$keyword}%' OR o.name LIKE '%{$keyword}%' OR o.mobile LIKE '%{$keyword}%' OR o.address LIKE '%{$keyword}%')";
		}
		if ($project_id) {
			$where .= " AND o.project_ids LIKE '%,{$project_id},%'";
		}
		if ($shop_id) {
			$where .= " AND o.shop_id='{$shop_id}'";
		}
		if (strlen($begin_date)) {
			$where .= " AND o.add_time>='".strtotime($begin_date)."'";
		}
		if (strlen($end_date)) {
			$where .= " AND o.add_time<='".strtotime($end_date)."'";
		}
		if (strlen($status)) {
			$where .= " AND o.status IN ({$status})";
		}
		if (strlen($pay_method)) {
			$where .= " AND o.pay_method='{$pay_method}'";
		}
		if (strlen($is_jiesuan)) {
			$where .= " AND o.is_jiesuan='{$is_jiesuan}'";
		}
		if (trim($order_id, ',')) {
			$where .= " AND o.id IN (".trim($order_id, ',').")";
		}
		
		$fields = array(
			'id'=>'ID',
			'order_sn'=>'订单号',
			'shop_name'=>'门店',
			'project_names'=>'订单项目',
			'name'=>'下单人',
			'service_time'=>'服务时间',
			'add_time'=>'下单时间',
			'paid_time'=>'支付时间',
			'total_price'=>'支付金额',
			'trans_fee'=>'交易费',
			'cash_coupon_money'=>'现金券',
			'pay_method_name'=>'支付方式',
			'status_name'=>'订单状态',
			'jiesuan_status'=>'结算状态',
			'jiesuan_price'=>'结算金额',
		);
		
		$sql = "SELECT o.*, s.name as shop_name, '' as pay_method_name, '' as project_names, 
			'5%' as trans_fee, '0' as jiesuan_price, IF(is_jiesuan=1,'已结算','未结算') as jiesuan_status 
			FROM jf_order o LEFT JOIN jf_beautyshop s ON o.shop_id=s.id 
			WHERE 1 {$where} ORDER BY o.id DESC";
		//exit($sql);
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$project_names = '';
				$sql = "SELECT project_id, project_name FROM jf_order_project WHERE order_id='{$row->id}'";
				$p = $this->db->get_results($sql);
				if ($p) {
					foreach ($p as $k => $r) {
						$project_names .= ($project_names!=''?'、':'') . $r->project_name;
					}
				}
				$rs[$key]->project_names = $project_names;
				$rs[$key]->order_sn = $row->order_sn.' ';
				$rs[$key]->pay_method_name = $this->order_mod->pay_method($row->pay_method);
				$rs[$key]->cash_coupon_money = '-'.$row->cash_coupon_money;
				$rs[$key]->add_time = date('Y-m-d',$row->add_time);
				$rs[$key]->paid_time = date('Y-m-d',$row->paid_time);
				$rs[$key]->jiesuan_price = '￥'.($row->total_price-$row->total_price*0.05);
				$rs[$key]->total_price = '￥'.$row->total_price;
				$rs[$key]->status_name = $this->order_mod->status_name($row->status);
			}
			export_excel($rs, $fields, "finance");
		}
	}
	
	//单个结算
	public function jiesuan() {
		$id = (isset($_GET['id'])) ? intval($_GET['id']) : 0;
		if ($id<=0) exit;
		
		$sql = "UPDATE jf_order SET is_jiesuan='1', jiesuan_time='".time()."' WHERE id='{$id}'";
		$this->db->query($sql);
		
		header("location:?app=finance&act=index");
	}
	
	//商家的申请提现列表
	public function apply() {
		$where = '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$begin_date = (isset($_GET['begin_date'])) ? trim($_GET['begin_date']) : '';
		$end_date = (isset($_GET['end_date'])) ? trim($_GET['end_date']) : '';
		
		if ($keyword) {
			$where .= " AND (s.name LIKE '%{$keyword}%' OR s.mobile LIKE '%{$keyword}%' OR s.address LIKE '%{$keyword}%')";
		}
		if (strlen($begin_date)) {
			$where .= " AND bw.add_time>='".strtotime($begin_date)."'";
		}
		if (strlen($end_date)) {
			$where .= " AND bw.add_time<='".strtotime($end_date)."'";
		}
		
		//分页
		$this->setpages(array('keyword'=>$keyword, 'begin_date'=>$begin_date, 'end_date'=>$end_date));
		
		$sql = "SELECT *  FROM jf_withdraw bw  WHERE 1 {$where} ORDER BY bw.id DESC";
		$rs = $this->ezr->get_results($sql);
		
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);

		$this->smarty->assign('is_admin', $_SESSION['admin']->is_admin);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('begin_date',$begin_date);
		$this->smarty->assign('end_date',$end_date);
		
		$this->display();
	}
	
	//审核申请
	public function audit() {
		$id = (isset($_POST['id'])) ? intval($_POST['id']) : 0;
		$status = (isset($_POST['status'])) ? intval($_POST['status']) : 0;
		$memo = (isset($_POST['memo']) && trim($_POST['memo'])) ? trim($_POST['memo']) : '';
		if ($id<=0) exit;
		
		$sql = "UPDATE jf_beautyshop_withdraw SET status='{$status}', memo='{$memo}', audit_time='".time()."' WHERE id='{$id}'";
		$this->db->query($sql);
		
		if ($status==1) {
			$sql = "SELECT orders FROM jf_beautyshop_withdraw WHERE id='{$id}'";
			$orders = $this->db->get_var($sql);
			$sql = "UPDATE jf_order SET withdraw_time='".time()."' WHERE id IN ({$orders})";
			$this->db->query($sql);
		}
		
		header("location:?app=finance&act=apply");
	}
}
